/**
 * 
 */
package JDBCTools;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author huangliusong E-mail:huangliusong1994@gmail.com
 * @version 创建时间：2018年7月16日 下午5:34:38 类说明
 */
public class MysqlUtils {
	public static void update(String sql) {

		Connection connection = null;
		Statement statement = null;
		try {
			connection = JDBCTools.getConnection();
			statement = connection.createStatement();
			System.err.println("执行sql=" + sql);
			statement.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.err.println("关闭连接");
			JDBCTools.release(statement, connection);
		}
	}

	public static List<Map<String, Object>> getData() {
		List<Map<String, Object>> list = new ArrayList<>();
		Connection connection = null;
		Statement statement = null;
		ResultSet rs = null;
		String filepath = "";
		String createtime = "";
		String sql = "SELECT * FROM newfile";
		try {
			connection = JDBCTools.getConnection();
			statement = connection.createStatement();
			rs = statement.executeQuery(sql);
			// 4.执行查询，得到ResultSet
			rs = statement.executeQuery(sql);
			// 5.处理ResultSet
			while (rs.next()) {
				Map<String, Object> map = new HashMap<>();
				// rs.get+数据库中对应的类型+(数据库中对应的列别名)
				filepath = rs.getString("filepath");
				createtime = rs.getString("createtime");
				map.put("filepath", filepath);
				map.put("createtime", createtime);
				list.add(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCTools.release(statement, connection);
		}
		return list;
	}

	public static boolean isData(String filePath) {
		Connection connection = null;
		Statement statement = null;
		ResultSet rs = null;
		String filepath = "";
		String sql = "SELECT * FROM newfile where filepath='" + filePath + "'";
		try {
			connection = JDBCTools.getConnection();
			statement = connection.createStatement();
			rs = statement.executeQuery(sql);
			// 4.执行查询，得到ResultSet
			rs = statement.executeQuery(sql);
			// 5.处理ResultSet
			while (rs.next()) {
				// rs.get+数据库中对应的类型+(数据库中对应的列别名)
				filepath = rs.getString("filepath");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCTools.release(statement, connection);
		}
		if (filepath.equals("") || filepath.equals(null)) {
			return true;
		} else {
			return false;
		}
	}

	public static List<Map<String, Object>> getDataOld() {
		List<Map<String, Object>> list = new ArrayList<>();
		Connection connection = null;
		Statement statement = null;
		ResultSet rs = null;
		String filepath = "";
		String createtime = "";
		String sql = "SELECT * FROM file";
		try {
			connection = JDBCTools.getConnection();
			statement = connection.createStatement();
			rs = statement.executeQuery(sql);
			// 4.执行查询，得到ResultSet
			rs = statement.executeQuery(sql);
			// 5.处理ResultSet
			while (rs.next()) {
				Map<String, Object> map = new HashMap<>();
				// rs.get+数据库中对应的类型+(数据库中对应的列别名)
				filepath = rs.getString("filepath");
				createtime = rs.getString("createtime");
				map.put("filepath", filepath);
				map.put("createtime", createtime);
				list.add(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCTools.release(statement, connection);
		}
		return list;
	}

	public static boolean isDataOld(String filePath) {
		Connection connection = null;
		Statement statement = null;
		ResultSet rs = null;
		String filepath = "";
		filePath = filePath.replace("\\", "\\\\");
		String sql = "SELECT * FROM file where filepath='" + filePath + "'";
		try {
			connection = JDBCTools.getConnection();
			statement = connection.createStatement();
			rs = statement.executeQuery(sql);
			// 4.执行查询，得到ResultSet
			rs = statement.executeQuery(sql);
			// 5.处理ResultSet
			while (rs.next()) {
				// rs.get+数据库中对应的类型+(数据库中对应的列别名)
				filepath = rs.getString("filepath");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCTools.release(statement, connection);
		}
		if (filepath.equals("") || filepath.equals(null)) {
			return true;
		} else {
			return false;
		}
	}
}
